//
//  ApplyForClaLeaTeaListViewController.m
//  AttendenceSys
//
//  Created by ld on 16/5/23.
//  Copyright © 2016年 窦芳俊. All rights reserved.
//

#import "ApplyForClaLeaTeaListViewController.h"

@interface ApplyForClaLeaTeaListViewController ()

@end

@implementation ApplyForClaLeaTeaListViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //self.strNavTitle = @"申请课时假老师";
    _appClassTeaArr = [[NSMutableArray alloc] init];
   // _sDate = [self.strNavTitle stringByReplacingOccurrencesOfString:@"." withString:@"-"];
    
    //    [self setRightCallButtonWithImageName:@"ic_drop" callPressedImageName:@"ic_drop" rightButtonImageName:@"ic_drop" rightPressedImageName:@"ic_drop"];
    [self initView];
    [self initData];
    //[self setRightButtonWithImageName:@"ic_drop" bgImageName:@""];
    //[self getStuCourseInfo:_sDate];
}

//获取初始化数据
- (void)initData
{
    [self queryTeacherListInfo];
}

- (void)initView
{
    //init tableView
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49 - 15);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollEnabled = YES;
    [_tableView registerClass:[AskForLeaTableViewCell class] forCellReuseIdentifier:NSStringFromClass([AskForLeaTableViewCell class])];
    [self.view addSubview:_tableView];
}

//查询老师的接口http://211.65.216.54/queryTeacherList.action
- (void)queryTeacherListInfo
{
    NSString* refer = @"queryTeacherList.action?";
    NSString* prefix = kASHttpUrl(refer);
    
    [SVProgressHUD showWithStatus:@"获取课程老师列表..." maskType:SVProgressHUDMaskTypeClear];
    ASNetworkEngine *networkingEngine = [ASNetworkEngine sharedInstance];
    
    [networkingEngine GET:prefix parameters:nil success:^(id responseObject) {
        
        NSDictionary* jsonRsp = (NSDictionary *)responseObject;
        jsonRsp = [jsonRsp processDictionaryIsNSNull:jsonRsp];
        if (isSuccessRsp(jsonRsp))
        {
            NSArray* data = [jsonRsp objectForKey:kASJsonData];
            _appClassTeaArr = [self parsequeryTeacherListInfo:data];
            [self.tableView reloadData];
            
            [SVProgressHUD dismiss];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:errorMsgRsp(jsonRsp)];
        }
        
    } failure:^(NSError *error) {
        
        //[SVProgressHUD showErrorWithStatus:error.description];
        [SVProgressHUD showErrorWithStatus:@"网络异常，请稍后再试"];
    }];
}

- (NSMutableArray *)parsequeryTeacherListInfo:(NSArray *)arr
{
    NSMutableArray* courseArray = [[NSMutableArray alloc] init];
    for (unsigned int i = 0; i < arr.count; i++)
    {
        AppForClaTeaInfo *classInfo = [[AppForClaTeaInfo alloc] init];
        NSDictionary* dic = [arr objectAtIndex:i];
        dic = [dic processDictionaryIsNSNull:dic];
        
        classInfo.userCode = [dic objectForKey:@"userCode"];
        classInfo.userId = [dic objectForKey:@"userId"];
        classInfo.userName = [dic objectForKey:@"userName"];
        
        [courseArray addObject:classInfo];
    }
    courseArray = [self sortArrDataSource:courseArray];
    return courseArray;
}

//排序
-(NSMutableArray*)sortArrDataSource:(NSMutableArray*)sourceDataArr
{
    //Step1:获取字符串中文字的拼音首字母并与字符串共同存放
    NSMutableArray *chineseStringsArray=[NSMutableArray array];
    NSMutableDictionary *chineseStringsDic=[NSMutableDictionary dictionary];
    for(int i=0;i<[sourceDataArr count];i++){
        ChineseString *chineseString=[[ChineseString alloc] init];
        AppForClaTeaInfo* appInfo = [sourceDataArr objectAtIndex:i];
        chineseString.string=[NSString stringWithString:appInfo.userName];
        chineseString.saveInfo = appInfo.userCode;
        if(chineseString.string==nil){
            chineseString.string=@"";
        }
        
        if(![chineseString.string isEqualToString:@""]){
            NSString *pinYinResult=[NSString string];
            for(int j=0;j<chineseString.string.length;j++){
                NSString *singlePinyinLetter=[[NSString stringWithFormat:@"%c",pinyinFirstLetter([chineseString.string characterAtIndex:j])]uppercaseString];
                
                pinYinResult=[pinYinResult stringByAppendingString:singlePinyinLetter];
            }
            chineseString.pinYin=pinYinResult;
        }else{
            chineseString.pinYin=@"";
        }
        [chineseStringsArray addObject:chineseString];
        if (0 < chineseString.saveInfo.length) {
            [chineseStringsDic setObject:appInfo forKey:chineseString.saveInfo];
        }
    }
    
    //Step3:按照拼音首字母对这些Strings进行排序
    NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"pinYin" ascending:YES]];
    [chineseStringsArray sortUsingDescriptors:sortDescriptors];
    
    // Step4:如果有需要，再把排序好的内容从ChineseString类中提取出来
    NSMutableArray *result=[NSMutableArray array];
    for(int i=0;i<[chineseStringsArray count];i++){
        NSString* chineseStr = ((ChineseString*)[chineseStringsArray objectAtIndex:i]).saveInfo;
        if (chineseStr && (0 < chineseStr.length)) {
            [result addObject:[chineseStringsDic objectForKey:chineseStr]];
        }
        //[result addObject:((ChineseString*)[chineseStringsArray objectAtIndex:i]).string];
    }
    
    return result;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_appClassTeaArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AskForLeaTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AskForLeaTableViewCell class])];
    [cell setBackgroundColor:[UIColor clearColor]];
    AppForClaTeaInfo * appForClaTeaInfo = [_appClassTeaArr objectAtIndex:indexPath.row];
    appForClaTeaInfo.userName = appForClaTeaInfo.userName ? appForClaTeaInfo.userName : @"";
    cell.titleLabel.text = appForClaTeaInfo.userName;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (_bCallName) {
        if (_delegate && [_delegate respondsToSelector:@selector(getCallNameTeaInfo:)]) {
            AppForClaTeaInfo* applyClaInfo = [_appClassTeaArr objectAtIndex:indexPath.row];
            [_delegate getCallNameTeaInfo:applyClaInfo];
        }
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    
    AppForClaTeaInfo* appClaInfo = [_appClassTeaArr objectAtIndex:indexPath.row];
    ApplyForClassLeaveViewController* appForClassViewCtrl = [[ApplyForClassLeaveViewController alloc] init];
    appForClassViewCtrl.appForTeaInfo = appClaInfo;
    appForClassViewCtrl.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:appForClassViewCtrl animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


@end




